package com.share.chat.controller;


import com.share.chat.domain.ResultInfo;
import com.share.chat.domain.User;
import com.share.chat.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author 谢佳豪
 * @data 2021/1/18 - 17:00
 * @description
 */
@Controller
@RequestMapping("/chat")
@ResponseBody
public class ChatController {

    private ChatService chatService;

    @Autowired
    public void setChatService(ChatService chatService) {
        this.chatService = chatService;
    }


    @GetMapping("/checkIsFirstChat")
    public ResultInfo checkIsFirstChat(HttpSession httpSession, @RequestParam String toUser){

        //获取httpSession中的user值

        User user = (User) httpSession.getAttribute("user");
        String fromUser = user.getUsername();

        boolean flag = chatService.isFirstChat(fromUser, toUser);

        if (flag) {
            return ResultInfo.successInfo("第一次进行聊天");
        } else {
            return ResultInfo.failInfo("不是第一次进行聊天");
        }
    }


    @GetMapping("/getChatList")
    public ResultInfo chatList( HttpSession httpSession) {

        //获取httpSession中的user值

        User user = (User) httpSession.getAttribute("user");
        String fromUser = user.getUsername();

        //获取当前用户的聊天列表返回
        return chatService.getFromUserChatList(fromUser);
    }

    @GetMapping("/getChatRecords/{toUser}")
    public ResultInfo recentChatRecords( @PathVariable String toUser, @RequestParam int startIndex, @RequestParam int pageSize, HttpSession httpSession){

        User user = (User) httpSession.getAttribute("user");
        String fromUser = user.getUsername();

        //获取最近的聊天记录
        return chatService.getRecentChatRecords(fromUser, toUser, startIndex, pageSize);

    }

    @GetMapping("/getPageNumber")
    public ResultInfo chatRecordsPageNumber(String linkId){

        return chatService.getPageNumber(linkId);
    }

    @GetMapping("/inWindows/{toUser}")
    public void updateIsSaveWindows(@PathVariable String toUser, HttpSession httpSession) {

        User user = (User) httpSession.getAttribute("user");
        String fromUser = user.getUsername();

        chatService.updateWindows(fromUser, toUser);
    }

    @GetMapping("/unread")
    public ResultInfo unreadTotalNumber(HttpSession httpSession) {

        // 获取httpSession中的user值
        User user = (User) httpSession.getAttribute("user");
        String username = user.getUsername();

        return chatService.getUnreadTotalNumber(username);

    }

    @GetMapping("/resetWindows")
    public ResultInfo resetWindows(HttpSession httpSession){

        // 获取httpSession中的user值
        User user = (User) httpSession.getAttribute("user");
        String username = user.getUsername();
        chatService.resetWindows(username);

        return ResultInfo.successInfo("重置成功！");
    }





}
